package com.atguigu.lease.web.app.custom.converter;

import com.atguigu.lease.model.enums.BaseEnum;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

/**
 * @author 芦博
 * @description
 * @createDate 2025/11/15 14:36
 */

public class customAppConverter implements ConverterFactory<String, BaseEnum> {
    @Override
    //声明任何类型继承BaseEnum就触发转换器(从String,转为换T类型)
    public <T extends BaseEnum> Converter<String, T> getConverter(Class<T> targetType) {
        return new Converter<String, T>() {
            @Override
            public T convert(String code) {
                System.out.println("APP转换器开始工作!!!");
                T[] arr = targetType.getEnumConstants();
                for (T s : arr) {
                    if (s.getCode().equals(Integer.valueOf(code))) {
                        return s;
                    }
                }
                throw new RuntimeException("枚举转换异常");
            }
        };
    }
}
